﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace RA.TextServices
{
    public static class HtmlUtilities
    {
        public static string GetHtmlContent(string url)
        {
            var webClient = new WebClient { Encoding = Encoding.Default };
            return webClient.DownloadString(url);
        }

        public static string GetPartOfHtml(string rawCode2, string beginKey, string endKey)
        {
            var startIndex = rawCode2.IndexOf(beginKey);
            if (startIndex < 0)
                return string.Empty;

            var endIndex = rawCode2.IndexOf(endKey, startIndex + beginKey.Length);
            if (endIndex < 0)
                return string.Empty;

            return rawCode2.Substring(startIndex + beginKey.Length, endIndex - startIndex - beginKey.Length);
        }

        public static XmlDocument FromHtml(TextReader reader)
        {
            // setup SgmlReader
            Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
            sgmlReader.DocType = "HTML";
            sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
            sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
            sgmlReader.InputStream = reader;
            // create document
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.XmlResolver = null;
            doc.Load(sgmlReader);
            return doc;
        }
    }
}
